package com.egao.oa.biz.impl;

import com.egao.common.core.damon.constant.UserConstant;
import com.egao.common.core.damon.results.BaseDao;
import com.egao.common.core.damon.results.PageResult2;
import com.egao.common.core.damon.utils.Utils;
import com.egao.common.core.web.JsonResult;
import com.egao.common.system.entity.User;
import com.egao.oa.biz.ProjectUpdateBiz;
import com.egao.oa.entity.ProjectUpdate;
import org.apache.poi.ss.formula.functions.T;
import org.springframework.jdbc.core.BeanPropertyRowMapper;
import org.springframework.jdbc.core.RowMapper;
import org.springframework.stereotype.Repository;

import java.util.HashMap;
import java.util.Map;
import java.util.List;

/**
 *
 * 数据层实现类
 * @author : Doman
 * @date : 2020-08-20 13:24:00
 * @version ：1.0
 */
@Repository
public class ProjectUpdateBizImpl extends BaseDao implements ProjectUpdateBiz {

	@Override
    public JsonResult list(PageResult2<T> page, ProjectUpdate projectUpdate, User loginUser) throws Exception {
        Map<String, String> map  = new HashMap<String, String>();
        RowMapper<ProjectUpdate> rm = new BeanPropertyRowMapper<ProjectUpdate>(ProjectUpdate.class);
        StringBuffer sql = new StringBuffer("select p.id,p.organization_id,p.name");
        sql.append(",dd.dict_data_name projectsName");
        sql.append(",p.content,p.edition,p.updateDate,p.remark");
		sql.append(",p.create_time,p.create_user,p.update_time,p.update_user");
        sql.append(",sogzn.organization_name organizationName,suser.username createName ");
        sql.append(" from oa_project_update p ");
        sql.append(" inner join sys_user suser on suser.user_id=p.create_user ");
        sql.append(" inner join sys_organization sogzn on sogzn.organization_id=p.organization_id ");
        sql.append(" left join sys_dictionary_data dd on dd.dict_data_code=p.name and dd.dict_id=3 ");
        sql.append(" where p.deleted=0 and sogzn.deleted=0 ");
        if(loginUser.getUserId() != UserConstant.ADMIN_ID){
            sql.append(" and p.organization_id=:tableOrganizationId ");
            map.put("tableOrganizationId", loginUser.getOrganizationId() + "");
        }
        if(Utils.isNotEmpty(projectUpdate)){
			if(Utils.isNotEmpty(projectUpdate.getContent())){
				sql.append(" and instr(p.content,:content)>0 ");
				map.put("content", projectUpdate.getContent());
			}
            if(Utils.isNotEmpty(projectUpdate.getSort())&&Utils.isNotEmpty(projectUpdate.getOrder())){
                sql.append(" order by p."+projectUpdate.getSort()+" "+projectUpdate.getOrder());
            } else {
                sql.append(" order by p.create_time desc");
            }
        }
        return jsonResult(getPageQueryList(page, sql.toString(), map, rm));
    }

    @Override
    public List<ProjectUpdate> list(ProjectUpdate projectUpdate, User loginUser) {
        Map<String, String> map  = new HashMap<String, String>();
        RowMapper<ProjectUpdate> rm = new BeanPropertyRowMapper<ProjectUpdate>(ProjectUpdate.class);
        StringBuffer sql = new StringBuffer("select p.id,p.organization_id,p.name");
        sql.append(",dd.dict_data_name projectsName");
        sql.append(",p.content,p.edition,p.updateDate,p.remark");
		sql.append(",p.create_time,p.create_user,p.update_time,p.update_user");
        sql.append(",sogzn.organization_name organizationName,suser.username createName ");
        sql.append(" from oa_project_update p ");
        sql.append(" inner join sys_user suser on suser.user_id=p.create_user ");
        sql.append(" inner join sys_organization sogzn on sogzn.organization_id=p.organization_id ");
        sql.append(" left join sys_dictionary_data dd on dd.dict_data_code=p.name and dd.dict_id=3 ");
        sql.append(" where p.deleted=0 and sogzn.deleted=0 ");
        if(loginUser.getUserId() != UserConstant.ADMIN_ID){
            sql.append(" and p.organization_id=:tableOrganizationId ");
            map.put("tableOrganizationId", loginUser.getOrganizationId() + "");
        }
        if(Utils.isNotEmpty(projectUpdate)){
//			if(Utils.isNotEmpty(projectUpdate.getName())){
//				sql.append(" and instr(p.name,:name)>0 ");
//				map.put("name", projectUpdate.getName());
//			}
            if(Utils.isNotEmpty(projectUpdate.getSort())&&Utils.isNotEmpty(projectUpdate.getOrder())){
                sql.append(" order by p."+projectUpdate.getSort()+" "+projectUpdate.getOrder());
            } else {
                sql.append(" order by p.create_time desc");
            }
        }
        getMapParameter(sql, map);
        return jdbcTemplate.query(sql.toString(),map, rm);
    }

    @Override
    public ProjectUpdate getById(ProjectUpdate projectUpdate) {
        RowMapper<ProjectUpdate> rm = new BeanPropertyRowMapper<ProjectUpdate>(ProjectUpdate.class);
        Map<String, String> map  = new HashMap<String, String>();
        StringBuffer sql = new StringBuffer("select p.id,p.organization_id,p.name");
        sql.append(",dd.dict_data_name projectsName");
        sql.append(",p.content,p.edition,p.updateDate,p.remark");
		sql.append(",p.create_time,p.create_user,p.update_time,p.update_user");
        sql.append(",sogzn.organization_name organizationName,suser.username createName ");
        sql.append(" from oa_project_update p ");
        sql.append(" inner join sys_user suser on suser.user_id=p.create_user ");
        sql.append(" inner join sys_organization sogzn on sogzn.organization_id=p.organization_id ");
        sql.append(" left join sys_dictionary_data dd on dd.dict_data_code=p.name and dd.dict_id=3 ");
        sql.append(" where p.deleted=0 and sogzn.deleted=0 and p.id=:id ");
        map.put("id", projectUpdate.getId());
        getMapParameter(sql, map);
        return jdbcTemplate.queryForObject(sql.toString(), map, rm);
    }

    @Override
    public boolean save(ProjectUpdate projectUpdate, User loginUser) {
        Map<String, String> map  = new HashMap<String, String>();
        StringBuffer sql =new StringBuffer("insert into oa_project_update (");
        sql.append("organization_id,name,content,edition");
		sql.append(",updateDate,remark,create_user,create_time) values ( ");
        sql.append(":organization_id,:name,:content,:edition");
		sql.append(",:updateDate,:remark,:create_user,now())");
        map.put("organization_id", loginUser.getOrganizationId() + "");
        map.put("name", projectUpdate.getName());
        map.put("content", projectUpdate.getContent());
        map.put("edition", projectUpdate.getEdition());
        map.put("updateDate", projectUpdate.getUpdatedate());
        map.put("remark", projectUpdate.getRemark());
        map.put("create_user", loginUser.getUserId() + "");
        getMapParameter(sql, map);
        int count = jdbcTemplate.update(sql.toString(), map);
        return count > 0;
    }

    @Override
    public boolean update(ProjectUpdate projectUpdate, User loginUser) {
        Map<String, String> map  = new HashMap<String, String>();
        StringBuffer sql =new StringBuffer("update oa_project_update set ");
        sql.append("name=:name,content=:content,edition=:edition ");
		sql.append(",updateDate=:updateDate,remark=:remark,update_time=now() ");
		sql.append(",update_user=:update_user where id=:id ");
        map.put("name", projectUpdate.getName());
        map.put("content", projectUpdate.getContent());
        map.put("edition", projectUpdate.getEdition());
        map.put("updateDate", projectUpdate.getUpdatedate());
        map.put("remark", projectUpdate.getRemark());
        map.put("update_user", loginUser.getUserId() + "");
        map.put("id", projectUpdate.getId());
        getMapParameter(sql, map);
        int count = jdbcTemplate.update(sql.toString(), map);
        return count > 0;
    }

    @Override
    public boolean remove(ProjectUpdate projectUpdate, User loginUser) {
        int count = 0;
        Map<String, String> map = null;
        if(projectUpdate.getId().length()>0){
            for(String ID : projectUpdate.getId().split(",")){
                map  = new HashMap<String, String>();
                if(ID==null || ID.equals("")){ break; }
                StringBuffer sql =new StringBuffer("update oa_project_update set deleted=1" +
                        ",update_user=:update_user, update_time=now() "
                        + " where id =:id ");
                map.put("update_user", loginUser.getUserId() + "");
                map.put("id", ID);
                getMapParameter(sql, map);
                count = jdbcTemplate.update(sql.toString(),map);
            }
        }
        return count > 0;
    }
}